<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace User\Controller;

use Zend\View\Model\ViewModel;
use Utilities\Session\SessionFacade;
use Catalog\Service\BasketService;
use User\Service\CustomerService;
use User\Service\CountryService;
use User\Service\OrderService;

/**
 */
class CheckoutController extends AbstractAuthenticatedController
{
    
    /**
     * 
     */
    protected $basketService;

    /**
     * 
     */
    protected $customerService;
    
    /**
     * 
     */
    protected $countryService;
    
    /**
     * 
     */
    protected $orderService;
    
    /**
     * 
     * @param BasketService $basketService
     * @param CustomerService $customerService
     * @param CountryService $countryService
     * @param OrderService $orderService
     */
    public function __construct(BasketService $basketService, CustomerService $customerService, CountryService $countryService,
        OrderService $orderService) 
    {
    	$this->basketService = $basketService;
    	$this->customerService = $customerService;
    	$this->countryService = $countryService;
    	$this->orderService = $orderService;
    }
    
    /**
     * 
     */
    public function summaryAction() 
    {
        $sessionFacade = new SessionFacade();

        return new ViewModel(array(
        	'locale' => $sessionFacade->getLocale(),
            'basket' => $this->basketService->get()
        ));
    }
    
    /**
     * 
     */
    public function checkCustomerAction()
    {
    	$sessionFacade = new SessionFacade();
    
    	$customer = $sessionFacade->getCustomer();
    	
    	// If customer is missing some mandatory values, present the form to be filled
    	if (is_null($customer->getFirstName()) || is_null($customer->getEmail()) || is_null($customer->getLastName()))
        	return new ViewModel(array(
    			'locale' => $sessionFacade->getLocale(),
                'basket' => $this->basketService->get()
    	));
    	
        return $this->redirect()->toRoute('user/checkout', array('action' => 'checkAddresses', 'controller' => 'checkout'), array(), true);
    }
    
    /**
     * 
     */
    public function updateCustomerAction() 
    {
        $this->customerService->updateCustomer($this->params()->fromPost('first_name'), $this->params()->fromPost('last_name'), 
            $this->params()->fromPost('email'));
        
        return $this->redirect()->toRoute('user/checkout', array('action' => 'checkAddresses', 'controller' => 'checkout'), array(), true);
    }
    
    /**
     * 
     */
    public function checkAddressesAction() 
    {
        $sessionFacade = new SessionFacade();
        
        $shippingAddress = null;
        $billingAddress = null;

        foreach ($this->customerService->getAddresses() as $address)
            if ($address->getType() == 'shipping')
                $shippingAddress = $address;
            else if ($address->getType() == 'billing')
                $billingAddress = $address;
        
        // Retrieve user addresses
        return new ViewModel(array(
        	'locale' => $sessionFacade->getLocale(),
            'shipping_address' => $shippingAddress,
            'billing_address' => $billingAddress,
            'basket' => $this->basketService->get(),
            'countries' => $this->countryService->getCountries(),
            'states' => $this->countryService->getStates()
        ));
    }
    
    /**
     * 
     */
    public function insertAddressAction() 
    {
        $country = $this->countryService->getCountryById($this->params()->fromPost('shipping_country_id'));
        $state = $this->countryService->getStateById($this->params()->fromPost('shipping_state_id'));
        
        // Insert or replace user addresses
        $this->customerService->insertOrUpdateAddresses($this->params()->fromPost('shipping_street'), $this->params()->fromPost('shipping_number'), 
            $this->params()->fromPost('shipping_city'), $this->params()->fromPost('shipping_zip_code'), $state->getId(), $state->getName(), $country->getId(), $country->getName());
        
        return $this->redirect()->toRoute('user/checkout', array('action' => 'preparePayment', 'controller' => 'checkout'), array(), true);
    }

    /**
     * 
     */
    public function preparePaymentAction() 
    {
        $sessionFacade = new SessionFacade();
        
        $basket = $this->basketService->get();
        
        return new ViewModel(array(
        	'locale' => $sessionFacade->getLocale(),
            'basket' => $basket,
            'customer_uid' => $sessionFacade->getCustomer()->getUid(),
            'shipping_tax' => $this->orderService->getShippingTax($basket['basket']->getSessionId()),
            'vat' => $this->orderService->getVat($basket['basket']->getId())
        ));
    }
    
}

?>